/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cloudminga.gui;

/**
 *
 * @author jyoti
 */

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import javax.swing.JFileChooser;
import com.globussoft.filesystem.*;

/*Class to create a XMl file */
class Xmlwritebackupfav {

    public void xmlwrite() throws IOException {
        Document document = new Document();
        Element root = new Element("files");
        Element child = new Element("fileproperty");
        int i = 0;
        String userName,osName,homeName,mydocPath;

        userName=System.getProperty("user.home");
        osName=System.getProperty("os.name");/* takes the name of operating System */



     homeName=System.getProperty("user.home");

            /// get Home Directory Path
            homeName=new File(homeName).getParent();


            System.out.println("home name : "+homeName);


            if (osName.equals("Windows Vista"))
            {
                mydocPath= userName + "\\Documents";
            }
            else
            {
                mydocPath= userName+"\\Favorites";
            }
        
       
        FileList filelist = new FileList();


        /*filelist are store in a hashmap*/
        HashMap hm = filelist.setRootDir(mydocPath);
        /*loop continues till the hash map is not Empty*/

        while (!hm.isEmpty()) {


            String filepath = (String) hm.get(i);
            System.out.println("file"+filepath);
            File filename = new File(filepath);

            Date date;
            String strDate;
            SimpleDateFormat sdf;
            JFileChooser filechooser = new JFileChooser(); //To get the FileType
            String filetype;
            String size = null;
            long kb = 0, gb = 0, mb = 0;
            long filesize;
            filetype = filechooser.getTypeDescription(filename);
            date = new Date();
            sdf = new SimpleDateFormat("dd/MM/yy hh:mm:ss");
            strDate = sdf.format(date);
            filesize = filename.length();
            filetype = filechooser.getTypeDescription(filename);


            //String ext = filename.substring(filename.lastIndexOf(".") + 1, filename.length());


            /*code to check the file size in Bytes,KB,MB,GB*/

            if (filesize < 1024) {
                size = String.valueOf(filesize) + "" + "bytes";
            } else if (filesize >= 1024 || filesize < 1024 * 1024) {
                kb = filesize / 1024;
                size = String.valueOf(kb) + " " + "KB";

            } else if (filesize >= 1024 * 1024 || filesize < 1024 * 1024 * 1024) {
                mb = filesize / (1024 * 1024);
                size = String.valueOf(mb) + " " + "MB";

            } else if (filesize >= 1024 * 1024 * 1024) {
                gb = filesize / (1024 * 1024 * 1024);
                size = String.valueOf(gb) + " " + "GB";

            }

            /*checks for textfile*/


            /*Sets the text for an Xml element*/
            child.addContent(new Element("filename").setText(filename.getName()));
            child.addContent(new Element("filepath").setText(filename.getPath()));
            child.addContent(new Element("filesize").setText(size));
            child.addContent(new Element("Type").setText(filetype));
            child.addContent(new Element("LastModified").setText(strDate));
            root.setContent(child);//add child to the root element
            document.setContent(root);//add root to the document


            FileWriter writer = new FileWriter("Xmlfile\\backupviewfav.xml");
            XMLOutputter outputter = new XMLOutputter();
            /* Set the XLMOutputter to pretty formatter. This formatter
             ** use the TextMode.TRIM, which mean it will remove the
             ** white-spaces */
            outputter.setFormat(Format.getPrettyFormat());

            /*write the document to the file*/
            outputter.output(document, writer);

            /*display on console*/
            outputter.output(document, System.out);
            i++;


        }//while loop closed here

    } /* xmlwrite() closed here */

}/*class close here */


public class FavoritesBackup {

    public static void main(String args[]) throws IOException {
        Xmlwritebackupfav xb = new Xmlwritebackupfav();
        xb.xmlwrite();


//        xmreadfile xl=new xmreadfile();
//        xl.readxm();

    }
}

